package com.anrongtec.zcpt.base;

import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;


/**
 * Created by Administrator on 2016/10/22.
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static CrashHandler mInstance=null;
    private CrashHandler(){

    }
    public static CrashHandler getInstance(){
        if(mInstance==null){
            mInstance = new CrashHandler();//一般在多线程下载的时候加同步锁，同一线程不需要，加了会降低效率
        }
        return  mInstance;
    }
    private Context mcontext;
    private  Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;

    public  void  init(Context context){
        this.mcontext = context;

        //获得捕获的管理器
        mDefaultUncaughtExceptionHandler =   Thread.getDefaultUncaughtExceptionHandler();
        //设置自己的类为管理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {//单一线程

        handlerException(thread, ex);
//        if(isHandler(ex)){
//            //自己处理
//            handlerException(thread, ex);
//        }else{
//            mDefaultUncaughtExceptionHandler.uncaughtException(thread,ex);
//        }

    }
    public  boolean isHandler(Throwable ex){
        if(ex==null){
            return  false;
        }else{
            return  true;
        }

    }

    private void handlerException(Thread thread, final Throwable ex) {
        new Thread(){
            @Override
            public void run() {
                Looper.prepare();
               /* new AlertDialog.Builder(DDApp.dApp.mContext)
                        .setTitle("标题")
                        .setMessage(ex.getMessage().toString())
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        })
                        .create().show();*/

                Toast.makeText(DDApp.dApp.mContext, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();

                Looper.loop();
            }
        }.start();

        //搜集异常信息
        collectionException(ex);

        try {
//            thread.sleep(500);
            //关闭activity
          // AppManager.getInstance().removeAll();
            //杀死进程
            Process.killProcess(Process.myPid());
            //关闭虚拟机
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**搜集异常**/
    private void collectionException(Throwable ex) {
        //获取信心发送到后台
        final String deviceInfo = Build.PRODUCT+ Build.DEVICE+ Build.MODEL;
        final String erroInfo = ex.getMessage();

        new Thread(){
            @Override
            public void run() {
                Looper.prepare();

                Log.e("ilove-Exception","deviceInfo:"+deviceInfo+"+ erroInfo:"+erroInfo);
                Looper.loop();
            }
        }.start();
    }
}
